package com.xgkj.diyiketang.activity;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.OrderInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.BuildOrderTask;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.util.WeiXinPay;
import com.xgkj.diyiketang.wxapi.WXPayEntryActivity;

public class ChongzhiXuefenActivity extends BaseActivity implements View.OnClickListener {
    private TextView back,xuefen1,xuefen2,xuefen3,xuefen4,xuefen5,xuefen6,chongzhi_xuefen;
    private Button chingzhi;
    private int orderTaskId = -1;
    private String orderNo;
    final IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
    private WeixinReceive mWeiXinReceive;
    private String orderTitle = "第一课堂";
    private String orderBody = "第一课堂";
    private ProgressDialog mProgress;
    private String total_money = "6";
    private String zongzichan_inte;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chongzhi_xuefen);
        initUI();
    }

    private void initUI() {

        chongzhi_xuefen = (TextView) findViewById(R.id.chongzhi_xuefen);
        back = (TextView) findViewById(R.id.zhuanyun_back);
        back.setOnClickListener(this);
        xuefen1 = (TextView) findViewById(R.id.chongzhi_xuefen1);
        xuefen1.setOnClickListener(this);
        xuefen2 = (TextView) findViewById(R.id.chongzhi_xuefen2);
        xuefen2.setOnClickListener(this);
        xuefen3 = (TextView) findViewById(R.id.chongzhi_xuefen3);
        xuefen3.setOnClickListener(this);
        xuefen4 = (TextView) findViewById(R.id.chongzhi_xuefen4);
        xuefen4.setOnClickListener(this);
        xuefen5 = (TextView) findViewById(R.id.chongzhi_xuefen5);
        xuefen5.setOnClickListener(this);
        xuefen6 = (TextView) findViewById(R.id.chongzhi_xuefen6);
        xuefen6.setOnClickListener(this);
        chingzhi = (Button) findViewById(R.id.live_prepare_btn);
        chingzhi.setOnClickListener(this);
        Intent intent = getIntent();
        if (intent.getStringExtra("zongzochan")!=null){
            zongzichan_inte = intent.getStringExtra("zongzochan");
            chongzhi_xuefen.setText(zongzichan_inte);
        }
    }
    //购买名额
    private void getOrder() {
        BuildOrderTask orderTask = new BuildOrderTask(null, "0", total_money, null, null, null, null, null, null, null, null);
        orderTask.addListener(new MyListener(mContext));
        orderTaskId = getEatShowApp().getTaskManager().addTask(orderTask);
    }
    class WeixinReceive extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            boolean isSuccess = intent.getBooleanExtra(ProtocolConst.WEIXINPAY_SUCCESS, false);
            if (isSuccess) {
                finish();
            } else {

            }

            if (mWeiXinReceive != null) {
                unregisterReceiver(mWeiXinReceive);
                mWeiXinReceive = null;
            }
        }
    }

    private void doWeixinPay() {
        if (!msgApi.isWXAppSupportAPI() || !msgApi.isWXAppInstalled()) {
            ToastUtil.showMessage(mContext, R.string.need_weixin_app);
            return;
        }

        if (mWeiXinReceive == null) {
            mWeiXinReceive = new WeixinReceive();
            IntentFilter filter = new IntentFilter();
            filter.addAction(WXPayEntryActivity.WEIXIN_PAY_SUCCESS_ACTION);
            registerReceiver(mWeiXinReceive, filter);
        }
        new WeiXinPay(msgApi, ChongzhiXuefenActivity.this, orderNo, orderBody, total_money);

//        finish();
    }

    private void dismissDialog() {
        if (mProgress != null && mProgress.isShowing()) {
            mProgress.dismiss();
            mProgress = null;
        }
    }
    class MyListener extends ShowToastTaskEventListener {
        public MyListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);

            if (orderTaskId == taskId) {
                if (data != null) {
                    OrderInfo orderInfo = (OrderInfo) data;
                    dismissDialog();
                    if (orderInfo != null) {
                        orderNo = orderInfo.getOrderNo();
                        doWeixinPay();
                    }
                }
            }

        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.zhuanyun_back:
                finish();
                break;
            case R.id.chongzhi_xuefen1:
//                xuefen1.setText("42学分");
//                xuefen2.setText("210学分");
//                xuefen3.setText("686学分");
//                xuefen4.setText("2086学分");
//                xuefen5.setText("4116学分");
//                xuefen6.setText("11186学分");
                total_money = "6";
                chingzhi.setText("立即支付6.00元");
                xuefen1.setTextColor(getResources().getColor(R.color.white));
                xuefen2.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen3.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen4.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen5.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen6.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen1.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                xuefen2.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen3.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen4.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen5.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen6.setBackgroundResource(R.drawable.chazhao_pop_btn);

                break;
            case R.id.chongzhi_xuefen2:
//                xuefen1.setText("42学分");
//                xuefen2.setText("210学分");
//                xuefen3.setText("686学分");
//                xuefen4.setText("2086学分");
//                xuefen5.setText("4116学分");
//                xuefen6.setText("11186学分");
                total_money = "30";
                chingzhi.setText("立即支付30.00元");
                xuefen1.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen2.setTextColor(getResources().getColor(R.color.white));
                xuefen3.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen4.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen5.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen6.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen1.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen2.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                xuefen3.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen4.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen5.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen6.setBackgroundResource(R.drawable.chazhao_pop_btn);
                break;
            case R.id.chongzhi_xuefen3:
//                xuefen1.setText("42学分");
//                xuefen2.setText("210学分");
//                xuefen3.setText("686学分");
//                xuefen4.setText("2086学分");
//                xuefen5.setText("4116学分");
//                xuefen6.setText("11186学分");
                total_money = "98";
                chingzhi.setText("立即支付98.00元");
                xuefen1.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen2.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen3.setTextColor(getResources().getColor(R.color.white));
                xuefen4.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen5.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen6.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen1.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen2.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen3.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                xuefen4.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen5.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen6.setBackgroundResource(R.drawable.chazhao_pop_btn);
                break;
            case R.id.chongzhi_xuefen4:
//                xuefen1.setText("42学分");
//                xuefen2.setText("210学分");
//                xuefen3.setText("686学分");
//                xuefen4.setText("2086学分");
//                xuefen5.setText("4116学分");
//                xuefen6.setText("11186学分");
                total_money = "298";
                chingzhi.setText("立即支付298.00元");
                xuefen1.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen2.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen3.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen4.setTextColor(getResources().getColor(R.color.white));
                xuefen5.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen6.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen1.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen2.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen3.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen4.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                xuefen5.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen6.setBackgroundResource(R.drawable.chazhao_pop_btn);
                break;
            case R.id.chongzhi_xuefen5:
//                xuefen1.setText("42学分");
//                xuefen2.setText("210学分");
//                xuefen3.setText("686学分");
//                xuefen4.setText("2086学分");
//                xuefen5.setText("4116学分");
//                xuefen6.setText("11186学分");
                total_money = "588";
                chingzhi.setText("立即支付588.00元");
                xuefen1.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen2.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen3.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen4.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen5.setTextColor(getResources().getColor(R.color.white));
                xuefen6.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen1.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen2.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen3.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen4.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen5.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                xuefen6.setBackgroundResource(R.drawable.chazhao_pop_btn);
                break;
            case R.id.chongzhi_xuefen6:
//                xuefen1.setText("42学分");
//                xuefen2.setText("210学分");
//                xuefen3.setText("686学分");
//                xuefen4.setText("2086学分");
//                xuefen5.setText("4116学分");
//                xuefen6.setText("11186学分");
                total_money = "1598";
                chingzhi.setText("立即支付1598.00元");
                xuefen1.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen2.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen3.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen4.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen5.setTextColor(getResources().getColor(R.color.zhuanyun_tv));
                xuefen6.setTextColor(getResources().getColor(R.color.white));
                xuefen1.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen2.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen3.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen4.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen5.setBackgroundResource(R.drawable.chazhao_pop_btn);
                xuefen6.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                break;
            case R.id.live_prepare_btn:

                getOrder();
                break;
        }
    }
}
